#include "view_heart_rate.h"
#include "app_trace.h"
#include "battery_ctrl.h"
#include "blecom.h"
#include "datetime.h"
#include "display_ctrl.h"
#include "display_interface.h"
#include "font_mngr.h"
#include "heart_rate_interface.h"
#include "lfs_file_manager_auto.h"
#include "math.h"
#include "platform_assert.h"
#include "rtc.h"
#include "sport_his.h"
#include "view_heart_rate.inc"
#include "weather_ctrl.h"
#include "font/font.h"
#include "prompt/view_prompt.h"
#define P_BAR_X_POS 2
#define P_BAR_Y_POS 12
#define P_UNIT_X_POS 45
#define P_UNIT_Y_POS 102
#define P_GIF_HEART_X_POS 38
#define P_GIF_HEART_Y_POS 12
#define P_VALUE_X_POS 38
#define P_VALUE_Y_POS 68
#define P_NOT_WEAR_ICON_X_POS 16
#define P_NOT_WEAR_ICON_Y_POS 38
#define P_DASH_X_POS 45
#define P_DASH_Y_POS 79
void view_heart_rate_animation(view_heart_rate_param const *param)
{

    if (!param->is_wear)
    {
        // gui_fbuf_draw_rectangle_by_gradients(0, 0, 80, 160, 0xfc8d, 0xfa43, true, origin);
        draw_on_chip_unzip_gradients_image_with_back_ground(0, 0, NULL, RGB565_COLOR_WHITE, 0xfc8d, 0xfa43);
        // gui_fbuf_draw_user_gray_image(P_NOT_WEAR_ICON_X_POS, P_NOT_WEAR_ICON_Y_POS, RGB565_COLOR_WHITE, zip_gif_not_wear[index % 2]);
        draw_on_chip_gray_gif_array_filter_color(P_NOT_WEAR_ICON_X_POS, P_NOT_WEAR_ICON_Y_POS, zip_gif_not_wear, sizeof(zip_gif_not_wear) / sizeof(zip_gif_not_wear[0]), RGB565_COLOR_WHITE);
        return;
    }
    draw_on_chip_unzip_gradients_image(P_BAR_X_POS, P_BAR_Y_POS, g_bar, 0xf28a, 0xf60a, RGB565_COLOR_BLACK);
    draw_on_chip_gray_gif_array(P_GIF_HEART_X_POS, P_GIF_HEART_Y_POS, zip_image_small_heart, sizeof(zip_image_small_heart) / sizeof(zip_image_small_heart[0]), 0xf28c);
    int num = param->second;
    if (param->second == 0)
    {
        num = param->heart_rate;
        draw_on_chip_gray_image(P_UNIT_X_POS, P_UNIT_Y_POS, zip_image_bpm, 0xf28c);
        if (num == 0)
        {
            draw_on_chip_unzip_gif_array(P_DASH_X_POS, P_DASH_Y_POS, g_gif_dash, sizeof(g_gif_dash) / sizeof(g_gif_dash[0]));
            return;
        }
        if (num < 40 || num > 220)
        {
            return;
        }
        int level = get_heart_rate_level(param->heart_rate);
        int width = 14;
        int height = 24;
        int x = 3;
        int y_array[] = {13, 38, 63, 88, 113};
        int color[] = {0xf28a, 0xf36a, 0xf46a, 0xf50a, 0xf60a};
        int y = 0;
        int max_count = sizeof(y_array) / sizeof(y_array[0]) - 1;
        int level_count = 0;
        switch (level)
        {
        case 0:
            level_count = 1;
            break;
        case 1:
            level_count = 2;
            break;
        case 2:
            level_count = 3;
            break;
        case 3:
            level_count = 4;
            break;
        case 4:
        case 5:
            level_count = 5;
            break;
        }
        for (int i = 0; i < level_count; i++)
        {
            y = y_array[max_count - i];
            draw_rectangle(x, y, x + width - 1, y + height - 1, color[max_count - i]);
        }
    }
    draw_on_chip_image_num_center(num, P_VALUE_X_POS, P_VALUE_Y_POS, P_VALUE_X_POS + 40 - 1, P_VALUE_Y_POS + 28 - 1, zip_curve_num_10x28, 10, 1, draw_type_normal);
}
void view_heart_rate_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.com_data.u_temp[0] = hr_get_hr_data();
        disp_ctrl.com_data.u_temp[1] = hr_if_band_wear();
    }
    bool is_wear = false;
    int heart_rate = 0;
#if 0
#warning debug
    is_wear = true;
    heart_rate = 132;
#else
    is_wear = disp_ctrl.com_data.u_temp[1];
    heart_rate = disp_ctrl.com_data.u_temp[0];
#endif
    view_heart_rate_param const param = {
        .is_wear = is_wear,
        .heart_rate = heart_rate,
        .second = 0,
    };

    view_heart_rate_animation(&param);
}
view_event_result_t view_heart_rate_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    case event_view_entry:
        disp_ctrl.auto_close_delay = 60000;
#if 0
#warning debug
#else
        hr_test_start(HR_CTRL_TYPE_HR_TEST);
#endif
        return view_event_result_handled;
    case event_view_re_screen_on:
        disp_ctrl.auto_close_delay = 60000;
        return view_event_result_handled;
    case event_view_exit:
        hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
